/**
 * This code is released under lgpl v3 license.
 * All rights reserved.
*/

package net.gfbridge.demo.client;


import bridge.FABridgeInstance;
import bridge.FABridgeWrapper;
import bridge.InitCallback;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.SpriteWrapper;
import flash.display.Stage;
import flash.display.StageWrapper;
import flash.events.AS3EventHandler;
import flash.events.Event;
import pl.rmalinowski.gwt2swf.client.ui.SWFWidget;

/**
 *
 * @author yuan yunchang    dukerr@gmail.com
 */
public class SpriteDemo implements EntryPoint{
    String SpriteDemo_bridgeName = "spriteDemo";
    // root object of bridge
    FABridgeInstance bridgeInstance;
    Sprite root;
    Button createSprite;
//    List<Sprite> sprites = new ArrayList<Sprite>();
    VerticalPanel mainLayout;
    
    public void onModuleLoad() {
        mainLayout = new VerticalPanel();
        mainLayout.setWidth("100%");
        RootPanel.get("sprite-demo").add(mainLayout);
        String baseUrl =GWT.getModuleBaseURL();
        SWFWidget swf = new SWFWidget(baseUrl+"EmptyFABridgeSWF.swf", "100%", "500px");
        swf.addFlashVar("bridgeName", SpriteDemo_bridgeName);
        mainLayout.add(swf);
        swf.addStyleName("flash-container");
        //RootPanel.get("sprite-demo").add(swf);

        FABridgeWrapper.get().addInitCallback(SpriteDemo_bridgeName, new InitCallback() {

            public void initComplete(FABridgeInstance inst) {
                //Window.alert("SpriteDemo init complete");
                bridgeInstance = inst;
                root = (Sprite)bridgeInstance.root();
                createControlsButtons();
            }
        });

        
    }
    
    void createControlsButtons(){
        createSprite = new Button("create sprite");
        createSprite.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {

                final double speed = Math.random() * 13;
                SpriteWrapper sw = new SpriteWrapper(SpriteDemo_bridgeName, true);
                final Sprite s = sw.getNativeAS3Obj();

                Graphics g = s.getGraphics();
                g.beginFill(0xFF0000);
                g.lineStyle(2, 2);
                g.drawRect(-100, -100, 200, 200);
                g.endFill();
                root.addChild(s);
                s.setX(300);
                s.setY(200);

                Stage stage = root.getStage();
                StageWrapper stagew = new StageWrapper(SpriteDemo_bridgeName, false);
                stagew.setNativeAS3Obj(stage);
                RotateHandler handler = new RotateHandler(s, speed);
                stagew.addHandler(handler, Event.ENTER_FRAME);

            }
        });
        mainLayout.add(createSprite);
    }
    class RotateHandler implements AS3EventHandler {

        Sprite sprite;
        double speed;

        public RotateHandler(Sprite psprite, double pspeed) {
            this.sprite = psprite;
            this.speed = pspeed;

        }

        public void onEvent(Event arg0) {
            double r = sprite.getRotation();
            sprite.setRotation(r + speed);
        }
    }

}
